using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using GameOnCopyBook.Rendering;
using GameOnCopyBook.Time;
using GameOnCopyBook.Sound;
using InputManagement;





namespace GameOnCopyBook.Menu
{
    /// <summary>
    /// Questo è un componente del gioco che implementa IUpdateable.
    /// </summary>
    public class Menu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
               
        RenderingData renderingData;
        ScoreManager scoreMgr = new ScoreManager();
        int bestScore;

        static public SoundManager soundMgr = new SoundManager();

        GameState.GameState gameState;
        float rotation;

        public Menu(Game game)
            : base(game)
        {
            // TODO: creare qui eventuali componenti figlio
            renderingData = Game.Services.GetService(typeof(RenderingData)) as RenderingData;    
        }
        public override void Initialize()
        {
            rotation = 0.0f;
            soundMgr.LoadContent(Game.Content);
            soundMgr.PlayInPause();
            base.Initialize();
        }

        protected override void Dispose(bool disposing)
        {
            Game.Services.RemoveService(typeof(RenderingData));
            base.Dispose(disposing);
        }

        protected override void LoadContent()
        {
            gameState = GameLogic.game_state;
            
            spriteBatch = new SpriteBatch(GraphicsDevice);
            renderingData.homeTexture = Game.Content.Load<Texture2D>(@"Textures\home1376x768");
            bestScore = scoreMgr.LoadBestTime();
            renderingData.font = Game.Content.Load<SpriteFont>("SpriteFont1");
            renderingData.font2 = Game.Content.Load<SpriteFont>("SpriteFont2");
            renderingData.creditsTexture = Game.Content.Load<Texture2D>(@"Textures\credits");
            renderingData.bigPentagonTexture = Game.Content.Load<Texture2D>(@"Textures\pentagonBig");
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            
            var dt = (float)gameTime.ElapsedGameTime.TotalSeconds;
            rotation += dt;
            if (InputManager.GetState().load)
            {
                Logic.Logic.LoadState();
                               
                while (Game.Components.Count > 0)
                {
                    ((GameComponent)Game.Components[0]).Dispose();
                }

                Game.Components.Add(new InputManager(Game));
                Game.Components.Add(new Logic.Logic(Game));
                Game.Components.Add(new Rendering.Rendering(Game));
                GameState.setState(gameState.Level);
            }
                       

            if (InputManager.GetState().click)
            {
                while (Game.Components.Count > 0)
                {
                    ((GameComponent)Game.Components[0]).Dispose();
                }

                Game.Components.Clear();

                Game.Components.Add(new InputManager(Game));
                Game.Components.Add(new Logic.Logic(Game));
                Game.Components.Add(new Rendering.Rendering(Game));
                GameLogic.resetState("Tutorial");
            }

            if (InputManager.GetState().exit)
            {
                Game.Exit();
            }
            
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(renderingData.homeTexture, new Rectangle(0, 0, (int)GraphicsDevice.Viewport.Width, (int)GraphicsDevice.Viewport.Height), Color.White);
                      
            //spriteBatch.DrawString(renderingData.font, " A prototype made by: Filippo Ferrando ", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f, 100.0f), Color.Black, 0.0f, renderingData.font.MeasureString(" A prototype made by: Filippo Ferrando ") / 2.0f, 1.0f, SpriteEffects.None, 0.5f);
            spriteBatch.DrawString(renderingData.font, "Best Score: " + bestScore + "sec", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f + 70.0f, 200.0f), Color.Black, 0.0f, renderingData.font.MeasureString(" Best score: -- sec ") / 2.0f, 1.4f, SpriteEffects.None, 0.5f);
            spriteBatch.DrawString(renderingData.font2, "Click To Start", new Vector2(Game.GraphicsDevice.Viewport.Width / 2.0f - 280.0f, 650.0f), Color.Black, 0.0f, renderingData.font.MeasureString(" Click To Start ") / 2.0f, 1.4f, SpriteEffects.None, 0.5f);

            spriteBatch.Draw(renderingData.bigPentagonTexture, new Vector2(Game.GraphicsDevice.Viewport.Width / 4.0f - 100.0f, 200.0f), null, Color.Red, rotation,
                new Vector2(renderingData.bigPentagonTexture.Width / 2.0f, renderingData.bigPentagonTexture.Height / 2.0f), 0.5f, SpriteEffects.None, 0.0f);

            spriteBatch.Draw(renderingData.bigPentagonTexture, new Vector2(Game.GraphicsDevice.Viewport.Width / 4.0f + 780.0f, 200.0f), null, Color.Red, -rotation,
                new Vector2(renderingData.bigPentagonTexture.Width / 2.0f, renderingData.bigPentagonTexture.Height / 2.0f), 0.5f, SpriteEffects.None, 0.0f);

            spriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
